package com.atguigu.binaryTree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class PostOrderInStack {
    public static List<Integer> postOrderUnRecur(TreeNode root){
        Stack<TreeNode> stack=new Stack<>();
        Stack<TreeNode> help=new Stack<>();
        if(root==null) return new ArrayList<>();
        List<Integer> list=new ArrayList<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode cur=stack.pop();
            help.push(cur);
            if(cur.left!=null){
                stack.push(cur.left);
            }
            if(cur.right!=null){
                stack.push(cur.right);
            }
        }
        //此时辅助栈里已经存放好逆序序列了
        while(!help.isEmpty()){
            list.add(help.pop().val);
        }
        return list;
    }
}
